package com.ecarx.vhaltests.hy11.testcases.climate;

import static com.ecarx.xui.adaptapi.car.hvac.IFragrance.HVAC_FUNC_AUTO_REFRESHING_FRAGRANCE;
import static com.ecarx.xui.adaptapi.car.property.IProperty.HVAC_FUNC_FRAGRANCE_REFRESH_POP_UP;
import static org.junit.Assert.assertEquals;

import android.car.VehicleAreaType;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.util.Log;

import androidx.test.platform.app.InstrumentationRegistry;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase;
import com.ecarx.xui.adaptapi.car.IWrapper;

import org.junit.BeforeClass;
import org.junit.Test;

public class HvacFuncRefreshPopUpTest extends VhalUnitTestBase {
    private static IWrapper.IPropertyId propertyId;
    protected static IWrapper wrapper;
    private static IpcpClient ipcpClient = new IpcpClient();
    private static final int AREA_ID = VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;

    private static boolean cbkPropValue;
    private static int cbkPropStatus;

    @BeforeClass
    public static void testInit() {
        wrapper = com.ecarx.xui.adaptapi.car.Car.createWrapper(InstrumentationRegistry.getInstrumentation().getTargetContext());
        propertyId = wrapper.getWrappedPropertyId(IWrapper.WrappedIdType.ID_TYPE_FUNCTION, HVAC_FUNC_FRAGRANCE_REFRESH_POP_UP);
        setConfig();
    }

    public boolean availability(){
        return mCarPropertyMgr.isPropertyAvailable(propertyId.getPropertyId(), AREA_ID);
    }

    public static void setConfig(){
        int ccSvcId = 135;
        int[] ccOperIds = {200};
        boolean ccSet = ipcpClient.setConfig(ccSvcId, ccOperIds);
        assertEquals(true, ccSet);

        int cemSvcId = 37;
        int[] cemOperId = {999};
        boolean cemResult = ipcpClient.setConfig(cemSvcId, cemOperId);
        assertEquals(true, cemResult);
        delay();
    }

    public void setCarConfig(boolean isAvail){
        int ccSvcId = 135;
        int ccOperIds = 200;
        int[] configs = {500, 505};
        int[] values = {isAvail? 2:1, isAvail? 2:1};
        boolean ccResult = ipcpClient.setCarconfigSignal(ccSvcId, ccOperIds, configs, values);
        delay();
        assertEquals(true, ccResult);
        Log.d("vli", "setCarConfig pass");
    }

    public void setCem(boolean isActive){
        int cemSvcId = 37;
        int cemOperId = 999;
        String[] cems = {"vehModMngtGlbSafe1UsgModSts","vehModMngtGlbSafe1CarModSts1"};
        int[] cemValues = {isActive? VehicleConstants.USG_MODE_CONVENIENCE:VehicleConstants.USG_MODE_ABANDON,
                VehicleConstants.CAR_MODE_DYNO};
        boolean cemUsgResult = ipcpClient.setCEMSignal(cemSvcId, cemOperId, cems, cemValues);
        delay();
        assertEquals(true, cemUsgResult);
        Log.d("vli", "setCem pass");
    }

    public void setAutoRefreshingActive(){
        setCarConfig(true);
        setCem(true);
    }

    public void setAutoRefreshingNotActive(){
        setCarConfig(true);
        setCem(false);
    }

    public void setAutoRefreshingOnOff(boolean isTrue){
        wrapper = com.ecarx.xui.adaptapi.car.Car.createWrapper(InstrumentationRegistry.getInstrumentation().getTargetContext());
        IWrapper.IPropertyId autoRefresh = wrapper.getWrappedPropertyId(IWrapper.WrappedIdType.ID_TYPE_FUNCTION, HVAC_FUNC_AUTO_REFRESHING_FRAGRANCE);
        setAutoRefreshingActive();
        delay();
        mCarPropertyMgr.setBooleanProperty(autoRefresh.getPropertyId(), AREA_ID, isTrue);
    }

    public void mockFragRefreshPopUpFrmMcu(boolean isOn){
        int cemSvcId = 37;
        int cemOperId = 999;
        String[] cems = {"vehModMngtGlbSafe1UsgModSts","vehModMngtGlbSafe1CarModSts1","fragRefreshPopUp"};
        int[] cemValues = {VehicleConstants.USG_MODE_CONVENIENCE, VehicleConstants.CAR_MODE_DYNO, isOn? 1:0};
        boolean cemUsgResult = ipcpClient.setCEMSignal(cemSvcId, cemOperId, cems, cemValues);
        delay();
        assertEquals(true, cemUsgResult);
        Log.d("vli", "setCem pass");
    }

    @Test
    public void testPopUpActive(){
        setCarConfig(true);
        setCem(true);
        int propStatus = mCarPropertyMgr.getProperty(propertyId.getPropertyId(),AREA_ID).getStatus();
        assertEquals(0, propStatus);
        assertEquals(true, availability());
    }

    @Test
    public void testPopUpNotActive(){
        setCarConfig(true);
        setCem(false);
        int propStatus = mCarPropertyMgr.getProperty(propertyId.getPropertyId(),AREA_ID).getStatus();
        assertEquals(1, propStatus);
    }
    @Test
    public void testPopUpNotAvail(){
        setCarConfig(false);
        assertEquals(false, availability());
    }

    @Test
    public void testPopUpTrue(){
        setAutoRefreshingActive();
        setAutoRefreshingOnOff(true);
        mockFragRefreshPopUpFrmMcu(true);
        delay();
        CarPropertyValue carProp = mCarPropertyMgr.getProperty(propertyId.getPropertyId(), AREA_ID);
        assertEquals(0, carProp.getStatus());
        assertEquals(true, carProp.getValue());
    }

    @Test
    public void testPopUpFalse(){
        setAutoRefreshingActive();
        setAutoRefreshingOnOff(true);
        mockFragRefreshPopUpFrmMcu(false);
        delay();
        CarPropertyValue carProp = mCarPropertyMgr.getProperty(propertyId.getPropertyId(), AREA_ID);
        assertEquals(0, carProp.getStatus());
        assertEquals(false, carProp.getValue());
    }

    @Test
    public void testAutoRefreshingCallback(){
        CarPropertyManager.CarPropertyEventCallback carPropertyEventCallback = new CarPropertyManager.CarPropertyEventCallback() {
            @Override
            public void onChangeEvent(CarPropertyValue carPropertyValue) {
                cbkPropValue = (boolean)carPropertyValue.getValue();
                cbkPropStatus = carPropertyValue.getStatus();
            }

            @Override
            public void onErrorEvent(int i, int i1) {

            }
        };

        mCarPropertyMgr.registerCallback(carPropertyEventCallback, propertyId.getPropertyId(), CarPropertyManager.SENSOR_RATE_ONCHANGE);
        CarPropertyValue<Boolean> carProp = mCarPropertyMgr.getProperty(Boolean.class, propertyId.getPropertyId(), AREA_ID);
        boolean oldPropValue = carProp.getValue();
        if(oldPropValue){
            //set notAvail
            setCarConfig(false);
            assertEquals(false, availability());
        }else{
            //set Active
            setAutoRefreshingActive();
            setAutoRefreshingOnOff(true);
            mockFragRefreshPopUpFrmMcu(true);
            delay();
            assertEquals(0, cbkPropStatus);
            assertEquals(true, cbkPropValue);
        }
        mCarPropertyMgr.unregisterCallback(carPropertyEventCallback);
    }

}
